package com.jieshuibao.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.model.domain.PageRequest;
import com.jieshuibao.model.domain.SearchResponse;
import com.jieshuibao.service.MessageService;
import com.jieshuibao.util.StringUtils;

@Controller
@RequestMapping("pushMessage")
public class MessageController {
	@Resource
	private MessageService messageService;
	/**
	 * 查看信息列表
	 * @param request
	 * @param paramsMap
	 * @return
	 */
	@RequestMapping(value = "/selectMessages", method = RequestMethod.POST)
	@ResponseBody
	public SearchResponse selectMessages(HttpServletRequest request,@RequestBody PageRequest pageRequest) {
		SearchResponse searchResponse = new SearchResponse();
		//获取用户信息
		CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
		if (cookieUser == null) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		//获取手机号码
		String phoneNumber = cookieUser.getPhoneNumber();
		if (StringUtils.isBlank(phoneNumber)) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		pageRequest.setPhone(phoneNumber);
		return messageService.selectMessagesByMessageType(pageRequest);
	}
	/**
	 * 查看信息详情
	 * @param request
	 * @param paramsMap
	 * @return
	 */
	@RequestMapping(value = "/selectMessageById", method = RequestMethod.POST)
	@ResponseBody
	public SearchResponse selectMessageById(HttpServletRequest request,@RequestBody Map<String, Object> paramsMap) {
		SearchResponse searchResponse = new SearchResponse();
		//获取用户信息
		CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
		if (cookieUser == null) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		//获取手机号码
		String phoneNumber = cookieUser.getPhoneNumber();
		if (StringUtils.isBlank(phoneNumber)) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		if((paramsMap.get("messageId") == null || "".equals(paramsMap.get("messageId")))
				&& (paramsMap.get("messageIds") == null || "".equals(paramsMap.get("messageIds")))){
			searchResponse.setCode("3");
			searchResponse.setDes("信息主键未发送");
			return searchResponse;
		}
		searchResponse.setObj(messageService.selectMessageById(paramsMap.get("messageId").toString()));
		return searchResponse;
	}
	/**
	 * 删除信息
	 * @param request
	 * @param paramsMap
	 * @return
	 */
	@RequestMapping(value = "/deleteMessageById", method = RequestMethod.POST)
	@ResponseBody
	public SearchResponse deleteMessageById(HttpServletRequest request,@RequestBody Map<String, Object> paramsMap) {
		SearchResponse searchResponse = new SearchResponse();
		//获取用户信息
		CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
		if (cookieUser == null) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		//获取手机号码
		String phoneNumber = cookieUser.getPhoneNumber();
		if (StringUtils.isBlank(phoneNumber)) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		if(paramsMap.get("messageId") == null || "".equals(paramsMap.get("messageId"))){
			searchResponse.setCode("3");
			searchResponse.setDes("信息主键未发送");
			return searchResponse;
		}
		int result = messageService.deleteMessageByMessageId(paramsMap.get("messageId").toString(),phoneNumber);
		if(result != 1){
			searchResponse.setCode("3");
			searchResponse.setDes("删除失败");
			return searchResponse;
		}
		return searchResponse;
	}
	/**
	 * 阅读业务推送消息
	 * @param request
	 * @param paramsMap
	 * @return
	 */
	@RequestMapping(value = "/updateMessagesForRead", method = RequestMethod.POST)
	@ResponseBody
	public SearchResponse updateMessagesForRead(HttpServletRequest request,@RequestBody Map<String, Object> paramsMap) {
		SearchResponse searchResponse = new SearchResponse();
		//获取用户信息
		CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
		if (cookieUser == null) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		//获取手机号码
		String phoneNumber = cookieUser.getPhoneNumber();
		if (StringUtils.isBlank(phoneNumber)) {
			searchResponse.setCode("2");
			searchResponse.setDes("重新登录");
			return searchResponse;
		}
		if(paramsMap.get("messageId") == null || "".equals(paramsMap.get("messageId"))){
			searchResponse.setCode("3");
			searchResponse.setDes("信息主键未发送");
			return searchResponse;
		}
		searchResponse = messageService.updateMessagesForRead(paramsMap.get("messageId").toString());
		return searchResponse;
	}
}
